module SDLBridge.SDLSprite;
private import SDL;
private import SDL_mixer;
private import opengl;
private import openglu;
private import SDLBridge.SDLGraphics;
private import SDLBridge.SDLTexture;

private import Graphics.IGraphics;
private import Graphics.ZSorter;
private import System.Console;
//private import std.ctype;
//private import std.string;

class SDLSprite : ISprite{
	private float _x,_y,_z;
	private float _width,_height;
	private float _opacity;
	private float _rotate;
	private bool _busy;
	private int _index;
	private SDLTexture _texture;
	private bool _visible;
	private static SDLTexture _blank;
	private Color _mask;
	
	private bool _flipV , _flipH;
	
	public this(){
		if( _blank is null ) _blank = SDLTexture.createTexture( "BLANK" , "_data/img/blank.bmp" );
		_texture = _blank;
		reset();
	} //

	public float x(){
		return _x;
	} //
	public void  x(float v){
		_x = x;
	} //
	public float y(){
		return _y;
	} //
	public void  y(float v){
		_y = v;
	} //
	public float z(){
		return _z;
	} //
	public void  z(float v){
		_z = v;
	} //
	public float width(){
		return _width;
	} //
	public void width(float v){
		_width = v;
	} //
	public float height(){
		return _height;
	} //
	public void height(float v){
		_height = v;
	} //

	public float opacity(){
		return _opacity;
	} //
	public void opacity( float o ){
		if ( o < 0 ) o = 0f;
		if ( o > 1 ) o = 1f;
		_opacity = o;
		int i = cast(int) ( o * 255f + .5 );
		_mask.a = i;
	} //

	public float rotate(){
		return _rotate;
	} //
	public void rotate(float r){
		_rotate = r;
	} //

	public bool busy(){
		return _busy;
	} //

	public ITexture texture(){
		return _texture;
	} //
	public void texture( ITexture tx ){
		SDLTexture s = cast(SDLTexture)tx;
		if ( s is null ) { 
			Console.writeln("Texture format error!");
			return;
		}
		_texture = s;
	} //

	public bool flipV(){
		return _flipV;
	} //
	public void flipV( bool f ){
		_flipV = f;
	} //
	public bool flipH(){
		return _flipH;
	} //
	public void flipH( bool f ){
		_flipH = f;
	} //

	public bool visible(){
		return _visible;
	} //
	public void visible( bool f ){
		_visible = f;
	} //
	public Color maskColor(){
		return _mask;
	} //
	public void maskColor(Color mask){
		_mask = mask;
	} //
	public void maskColor(int mask){
		_mask.color = mask;
	} //

	public void call(){
		reset();
		_busy = true;
	} //
	public void release(){
		reset();
		_texture = _blank;
		_busy = false;
	} //

	public void reset(){
		_flipV = false;
		_flipH = false;
		_opacity = 1;
		_x = 0;
		_y = 0;
		_z = 0;
		_width  = 16;
		_height = 16;
		_rotate = 0;
		_mask.color = 0xffffffffu;
		_visible = false;
		_texture = _blank;
	} //

	public bool draw( IGraphics g ){
		float x , y , w , h ;
		x = _x;
		y = _y;
		w = _width;
		h = _height;
		if ( flipH ) {
			x = x + w;
			w *= -1;
		}
		if ( flipV ) {
			y  = y + h;
			h *= -1;
		}
		g.drawImage( _texture , x , y , w , h , _mask ,_rotate );
		return true;
	} //
	public bool drawingEntry( IGraphics g ){
		if( _visible )
			return g.addDrawingEntry( this );
		return false;
	} //

	public void moveTo( float xx , float yy ){
		_x = xx;
		_y = yy;
	} //
	public void moveBy( float xx , float yy ){
		moveTo( _x + xx , _y + yy );
	} //

	public void setSize( float ww , float hh ) { 
		_width  = ww;
		_height = hh;
	} //

	invariant{
		assert( !( _texture is null ) );
	}
} //






